﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using com.mservice.momo.msg;
using System.IO;
using ProtoBuf;
using MoMoTransferVN_WP.utils;
using System.Reflection;
using System.Collections.Generic;

namespace MoMoTransferVN_WP
{
    public class MoMoReq
    {
        //build a momo message
        public static MomoMessage buildMoMoMessage(int cmdType, long cmdIndex, byte[] cmdBody)
        {
            String phoneNumber = SettingUtils.readString(Assets.KEY_PHONE_NUMBER);
            if (phoneNumber.StartsWith("0"))
            {
                phoneNumber = phoneNumber.Substring(1, phoneNumber.Length-1);
            }
                    
            int phone = 0;
            if (phoneNumber.Length > 0)
            {
                if (MoMoUtils.IsNumeric(phoneNumber))
                {
                    phone = Convert.ToInt32(phoneNumber);
                }
                else
                {
                    phone = MoMoUtils.GetNumber(phoneNumber);
                }
            }

            MomoMessage msg = new MomoMessage(cmdType, cmdIndex, phone, cmdBody);
            return msg;
        }

        //process hello message
        public static MomoMessage processHello()
        {

            string imeiKey = SettingUtils.readString(Assets.KEY_IMEI_KEY);

            Hello.Builder helloBuilder = Hello.CreateBuilder();
            helloBuilder.SetImei(DevicesUtils.getImei());
            helloBuilder.SetImeiKey(imeiKey);
            helloBuilder.SetSessionKey(App.mSessionKey);

            Hello hello = helloBuilder.Build();

            byte[] cmdBody = hello.ToByteArray();

            MomoMessage msg = buildMoMoMessage((int)MsgType.HELLO, App.getNextMsgId(), cmdBody);
            return msg;
        }
        //Process check phone exist
        public static MomoMessage processCheckPhone_Exist(long cmdIndex, string phonenumber)
        {
            PhoneExist phoneexist = PhoneExist.CreateBuilder().SetNumber(phonenumber).Build();
            byte[] body = phoneexist.ToByteArray();
            MomoMessage msg = buildMoMoMessage((int)MsgType.PHONE_EXIST, cmdIndex, body);
            return msg;
        }
        //process send devices info
        public static MomoMessage processSendDevicesInfo()
        {

            string deviceName = Microsoft.Phone.Info.DeviceStatus.DeviceName;
            string deviceVersion = Microsoft.Phone.Info.DeviceStatus.DeviceFirmwareVersion;
            string deviceModel = Microsoft.Phone.Info.DeviceStatus.DeviceHardwareVersion;
            string manufacturer = Microsoft.Phone.Info.DeviceStatus.DeviceManufacturer;
            string appVersion = DevicesUtils.GetAppVersion();

            string widthScreen = "";
            string heightScreen = "";
            /*
            System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                widthScreen = Application.Current.Host.Content.ActualWidth.ToString();
                heightScreen = Application.Current.Host.Content.ActualHeight.ToString();
            });
             */

            string devicePrimaryEmail = "";
            string tokenId = "";
            DeviceInfo deviceInfo = DeviceInfo.CreateBuilder()
                                             .SetDeviceName(deviceName)
                                             .SetDeviceVersion(deviceVersion)
                                             .SetDeviceModel(deviceModel)
                                             .SetDeviceManufacturer(manufacturer)
                                             .SetAppVersion(appVersion)
                                             .SetDeviceSWidth(widthScreen)
                                             .SetDeviceSHeight(heightScreen)
                                             .SetDevicePrimaryEmail(devicePrimaryEmail)
                                             .SetOs("WINDOWS PHONE")
                                             .SetToken(tokenId).Build();

            byte[] cmdBody = deviceInfo.ToByteArray();
            MomoMessage msg = buildMoMoMessage((int)MsgType.DEVICE_INFO, App.getNextMsgId(), cmdBody);
            return msg;
        }

        //process get otp
        public static MomoMessage processGetOtp()
        {
            MomoMessage msg = buildMoMoMessage((int)MsgType.GET_OTP, App.getNextMsgId(), null);
            return msg;
        }

        //process vetify otp
        public static MomoMessage processVetifyOtp(
                                        long cmdIndex,
                                        string otp)
        {
            VerifyOtp vetifyOtp = VerifyOtp.CreateBuilder().SetOtp(otp).Build();
            byte[] cmdBody = vetifyOtp.ToByteArray();
            MomoMessage msg = buildMoMoMessage((int)MsgType.VERIFY_OTP, cmdIndex, cmdBody);
            return msg;
        }

        //process login
        public static MomoMessage processLogIn(
                                     long cmdIndex,
                                     string mPin,
                                     string deviceModel)
        {
            LogIn login = LogIn.CreateBuilder().SetMpin(mPin).SetDeviceModel(deviceModel).Build();
            byte[] cmdBody = login.ToByteArray();
            MomoMessage msg = buildMoMoMessage((int)MsgType.LOGIN, cmdIndex, cmdBody);
            return msg;
        }

        //process signup
        public static MomoMessage processSignUp(
                                    long cmdIndex,
                                    string mPin,
                                    string mFullName,
                                    string mCMND,
                                    string mEmail,
                                    string mQuestion,
                                    string mAnswer,
                                    string mFriendNumber,
                                    string bday,
                                    string address)
        {
            Register reg = Register.CreateBuilder().SetPin(mPin)
                                                    .SetName(mFullName)
                                                    .SetCardId(mCMND)
                                                    .SetDob(bday)
                                                    .SetAddress(address)
                                                    .SetQuestion(mQuestion)
                                                    .SetAnswer(mAnswer)
                                                    .SetReference(mFriendNumber)
                                                    .Build();
            byte[] cmdBody = reg.ToByteArray();
            MomoMessage msg = buildMoMoMessage((int)MsgType.REGISTER, cmdIndex, cmdBody);
            return msg;
        }

        //process get bill info
        public static MomoMessage processGetBillInfo(long cmdIndex,
                                            string providerId,
                                            string billId,
                                            int actionGet)
        {
            GetBillInfo getBillInfo = GetBillInfo.CreateBuilder().SetProviderId(providerId).SetBillId(billId).SetGetBillInfoAction(actionGet).Build();
            byte[] cmdBody = getBillInfo.ToByteArray();
            MomoMessage msg = buildMoMoMessage((int)MsgType.GET_BILL_INFO, cmdIndex, cmdBody);
            return msg;
        }

        //process change password
        public static MomoMessage processChangeMPin(long cmdIndex,
                                         string oldPin,
                                         string newPin)
        {

            ChangePin changePin = ChangePin.CreateBuilder().SetOldPin(oldPin).SetNewPin(newPin).Build();
            byte[] cmdBody = changePin.ToByteArray();
            MomoMessage msg = buildMoMoMessage((int)MsgType.CHANGE_PIN, cmdIndex, cmdBody);
            return msg;
        }

        //process who is momoers
        public static MomoMessage processIsMoMoer(long cmdIndex,
                                       List<uint> phoneNumbers)
        {

            WhoIsMomoer.Builder whoIsMoMo = WhoIsMomoer.CreateBuilder();
            for (int i = 0; i < phoneNumbers.Count; i++)
            {
                whoIsMoMo.AddNumbers(phoneNumbers[i]);
            }
            byte[] cmdBody = whoIsMoMo.Build().ToByteArray();
            MomoMessage msg = buildMoMoMessage((int)MsgType.WHO_IS_MOMOER, cmdIndex, cmdBody);
            return msg;
        }

        

    }
}
